package com.mine.test.graphql.dataFetcher;

import com.mine.test.model.User;
import com.mine.test.service.IUserService;
import com.mine.test.service.UserServiceImpl;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * Project Name is testql.
 * User: wwq
 * Date: 2017/6/2
 * Time: 下午2:15
 */
@Component
public class UserDataFetcher implements DataFetcher, ApplicationContextAware {

    private static ApplicationContext context;

    public Object get(DataFetchingEnvironment environment) {
        IUserService userService = context.getBean(UserServiceImpl.class);
        Integer i=environment.getArgument("id");
        Long id = new Long(i);
        User user = userService.findById(id);
        return user;
    }

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }
}